﻿///////////////////////////////////////////////////////////////////////////////////
/// 文 件 名: MaterialsProcurements.cs
/// 作    者: XXD
/// 创建日期: 2012/6/19 8:58:15
/// 
///---------------------------------修改记录--------------------------------------
/// 修 改 人:          
/// 修改日期:
/// 修改原因: 
//////////////////////////////////////////////////////////////////////////////////
namespace ZX.BPM.Library
{
    using System;
    using System.Data.SqlClient;
    using System.Linq;
    using Csla;
    using Csla.Data;
    using ZX.DAL;

    [Serializable()]
    public class ReimbursementExpenses : BusinessListBase<ReimbursementExpenses, ReimbursementExpense>
    {
        public void Update(ReimbursementExpense reimbursementExpense)
        {
            foreach (ReimbursementExpense p in this)
            {
                if (p.ID == reimbursementExpense.ID)
                {
                    p.Amount = reimbursementExpense.Amount;
                    p.Explain = reimbursementExpense.Explain;
                    p.ReimbursementCategory = reimbursementExpense.ReimbursementCategory;
                    p.ReimbursementContent = reimbursementExpense.ReimbursementContent;
                    p.StartDate = reimbursementExpense.StartDate;
                    break;
                }
            }

        }

        public void Add(ReimbursementExpense reimbursementExpense)
        {
            if (!(Contains(reimbursementExpense.ID)))
            {
                ReimbursementExpense p = ReimbursementExpense.NewReimbursementExpense();
                p.Amount = reimbursementExpense.Amount;
                p.DemandID = reimbursementExpense.DemandID;
                p.Explain = reimbursementExpense.Explain;
                p.StartDate = reimbursementExpense.StartDate;
                p.ReimbursementContent = reimbursementExpense.ReimbursementContent;
                p.ReimbursementCategory = reimbursementExpense.ReimbursementCategory;
               // p.ID = Guid.NewGuid();
                base.Add(p);
            }
            else
            {
                throw new InvalidOperationException("Resource already assigned to project");
            }
        }
        public void Remove(Guid reimbursementExpenseID)
        {
            var item = (from r in this
                        where r.ID == reimbursementExpenseID
                        select r).FirstOrDefault();
            if (item != null)
            {
                Remove(item);
            }
        }

        public bool Contains(Guid reimbursementExpenseID)
        {
            var item = (from r in this
                        where r.ID == reimbursementExpenseID
                        select r).Count();
            return item > 0;
        }

        public bool ContainsDeleted(Guid reimbursementExpenseID)
        {
            var item = (from r in DeletedList
                        where r.ID == reimbursementExpenseID
                        select r).Count();
            return item > 0;
        }

        private void Child_Fetch(Guid demandID)
        {
            using (var ctx = ConnectionManager<SqlConnection>.GetManager(ConnectionStringNames.OpenExpressApp))
            {
                DAL.DBExtend.DbSql db = new DAL.DBExtend.DbSql();
                string strSql = string.Format("SELECT * FROM dbo.bpm_ReimbursementExpenses WHERE DemandID='{0}'", demandID);
                System.Data.DataTable data = db.ExecuteQuery(strSql);

                var rlce = RaiseListChangedEvents;
                RaiseListChangedEvents = false;
                foreach (System.Data.DataRow item in data.Rows)
                    base.Add(DataPortal.FetchChild<ReimbursementExpense>(new Guid(item["ID"].ToString())));
                RaiseListChangedEvents = rlce;
            }
        }


        #region  Factory Methods

        internal static ReimbursementExpenses NewReimbursementExpenses()
        {
            return DataPortal.CreateChild<ReimbursementExpenses>();
        }


        private ReimbursementExpenses()
        { /* require use of factory methods */ }

        #endregion
    }
}
